package com.hckjsec.cloud.message;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hckjsec.common.image.CenerateSignImage;
import com.hckjsec.common.utils.ComUtil;
import com.hckjsec.common.utils.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
public class SendWxMessage {
    //获取AccessToken
    public static void getAccessToken(TempToken tempToken,boolean reset) {
        String Access_Token= StringUtils.isHcNull(ComUtil.getTokenByAppidMap.get(tempToken.getAppid()),"");
        try {
            if ("".equals(Access_Token) || reset){
                RestTemplate restTemplate = new RestTemplate();
                Map<String, String> params = new HashMap<>();
                params.put("APPID", tempToken.getAppid());
                params.put("APPSECRET", tempToken.getSecret());
                ResponseEntity<String> responseEntity = restTemplate.getForEntity(ComUtil.WX_API_BASE+ComUtil.WX_ACCESSTOKEN+"?grant_type=client_credential&appid={APPID}&secret={APPSECRET}", String.class, params);
                String body = responseEntity.getBody();
                JSONObject object = JSON.parseObject(body);
                Access_Token = object.getString("access_token");
                ComUtil.getTokenByAppidMap.put(tempToken.getAppid(),Access_Token);
            }
        }catch(Exception e) {
            e.printStackTrace();
            System.out.println("获取Access_Token凭证出错=======");
        }
        tempToken.setAccess_token(Access_Token);
    }



    //模板消息-发送
    public static JSONObject Push(TempToken tempToken,boolean isReset) {
        RestTemplate restTemplate = new RestTemplate();
        String url = ComUtil.WX_API_BASE+ComUtil.WX_MESSAGE+"?access_token=" + tempToken.getAccess_token();
        WxMssVo wxMssVo = new WxMssVo();
        wxMssVo.setTouser(tempToken.getOpenid());
        wxMssVo.setTemplate_id(tempToken.getTemplate_id());
        wxMssVo.setPage(tempToken.getPage());
        wxMssVo.setData(tempToken.getData());
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, wxMssVo, String.class);
        JSONObject result = JSON.parseObject(responseEntity.getBody());
        String errCode = String.valueOf(result.get("errcode"));
        if(errCode.equals("40001") || errCode.equals("42001") || errCode.equals("40014")) {
            if (!isReset) {
                getAccessToken(tempToken,true);
                result=Push(tempToken,true);
            }
        }
        return result;
    }


    //获取小程序码
    public static boolean getCode(TempToken tempToken,boolean isReset,String accessLink,String path){
        boolean flag = false;
        try {
            RestTemplate restTemplate = new RestTemplate();
            String url = ComUtil.WX_API_BASE+ComUtil.WX_CODEUNLIMIT+"?access_token=" + tempToken.getAccess_token();
            WxMssCodeVo wxMssVo = new WxMssCodeVo();
            wxMssVo.setScene("accessLink="+accessLink);
            wxMssVo.setWidth(430);
            ResponseEntity<byte[]> responseEntity = restTemplate.postForEntity(url, wxMssVo, byte[].class);
            byte[] result = responseEntity.getBody();
            CenerateSignImage.ByteToFile(result,path);
            flag = true;
        }catch(Exception e) {
            System.out.println("获取小程序码出错=======");
            flag = false;
        }
        return flag;
    }
}
